//
//  YCTabbarViewController.m
//  YCVillageDoctor
//
//  Created by zhangxiaoliang on 2017/6/7.
//  Copyright © 2017年 zhangxiaoliang. All rights reserved.
//

#import "YCTabbarViewController.h"
#import "YCHomeViewController.h"
#import "YCMineViewController.h"
#import "YCReadingViewController.h"
#import "YCSellCardsViewController.h"
#import "YCNavViewController.h"
//分隔线常用颜色(灰色)
#define kSeparateColor HexColor(0xe2e2e2)

#define kSeperateLineWidth 0.5

@interface YCTabbarViewController ()<UITabBarControllerDelegate>

@end

@implementation YCTabbarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
//    self.delegate = self;

    self.tabBar.barTintColor = [UIColor whiteColor];
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kSeperateLineWidth)];
    lineView.backgroundColor = HexColor(0xcdcdcd);
    [self.tabBar addSubview:lineView];
    
    self.tabBar.barStyle = UIBarStyleBlack;
    self.tabBar.translucent = YES;
    
    
    //1.创建子控制器
    // 添加子控制器
    YCHomeViewController *homeVc = [[YCHomeViewController alloc]init];
//    [self addChildViewController:homeVc andWithTitle:@"首页" andWithImage:@"首页（未选中）" andWithSelectedImage:@"首页（选中）" andWithTag:100];
    
    YCSellCardsViewController *sellcardsVc = [[YCSellCardsViewController alloc] init];
    [self addChildViewController:sellcardsVc andWithTitle:@"售卡" andWithImage:@"售卡（未选中）" andWithSelectedImage:@"售卡（选中）" andWithTag:101];
    
//    YCReadingViewController *readingrVc = [[YCReadingViewController alloc] init];
//    [self addChildViewController:readingrVc andWithTitle:@"阅片" andWithImage:@"阅片（未选中）" andWithSelectedImage:@"阅片（选中）" andWithTag:102];
    
    YCMineViewController *mineVc = [[YCMineViewController alloc] init];
    [self addChildViewController:mineVc andWithTitle:@"我的" andWithImage:@"我的（未选中）" andWithSelectedImage:@"我的（选中）" andWithTag:103];
}
/**
 *  添加子控制器
 *
 *  @param childViewController 子控制器
 *  @param title               标题
 *  @param image               默认图片
 *  @param selectedImage       选中图片
 */
- (void)addChildViewController:(UIViewController *)childViewController andWithTitle:(NSString *)title andWithImage:(NSString *)image andWithSelectedImage:(NSString *)selectedImage andWithTag:(NSInteger)tag
{
    // 设置UITabBarController的tabBarItem的标题
    childViewController.title = title;
    // 对选中文字的处理
    NSMutableDictionary *textAttrs = [NSMutableDictionary dictionary];
    textAttrs[NSForegroundColorAttributeName] = [UIColor grayColor];
    textAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:10];;
    
    [childViewController.tabBarItem setTitleTextAttributes:textAttrs forState:UIControlStateNormal];
    
    NSMutableDictionary *textSelectAttrs = [NSMutableDictionary dictionary];
    textSelectAttrs[NSForegroundColorAttributeName] = RGB(77, 131, 232);
    textSelectAttrs[NSFontAttributeName] = [UIFont systemFontOfSize:10];
    [childViewController.tabBarItem setTitleTextAttributes:textSelectAttrs forState:UIControlStateSelected];
    
    childViewController.tabBarItem.tag = tag;
    childViewController.tabBarItem.image = [UIImage imageNamed:image];
    // 对选中图片的处理，imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal
    // 按照图片原来的样子显示出来，不需要系统自动渲染成其他颜色。
    childViewController.tabBarItem.selectedImage = [[UIImage imageNamed:selectedImage] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    // 包装导航控制
    YCNavViewController *navController = [[YCNavViewController alloc] initWithRootViewController:childViewController];
//    childViewController.tabBarItem.tag = 1;
    
    // 添加子控制器，把导航控制器作为UITabBarController的子控制器。
    [self addChildViewController:navController];
}

//-(BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
//    
//    NSUserDefaults *def=  [NSUserDefaults standardUserDefaults];
//    NSString *expert_id= [def objectForKey:KEXPERTID];
//    NSString * check_flag = [def objectForKey:KCHECKFLAG];
//    
//    if (expert_id && [check_flag isEqualToString:@"1"]) {
//        
//        return YES;
//    }else{
//        
//        BOOL isShow;
//        if (viewController.tabBarItem.tag == 103 || viewController.tabBarItem.tag == 100) {
//            
//            isShow = YES;
//            
//        }else{
//            
//            if (tabBarController.selectedIndex == 0) {
//                
//                [[NSNotificationCenter defaultCenter] postNotificationName:@"HomeToAuthentication" object:@(viewController.tabBarItem.tag)];
//                
//            }else if(tabBarController.selectedIndex == 3){
//                
//                [[NSNotificationCenter defaultCenter] postNotificationName:@"Authentication" object:nil];
//            }
//            isShow = NO;
//            
//        }
//        return isShow;
//    }
//}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
